package com.itcat.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcat.api.mysql.UmsAdminRoleApi;
import com.itcat.api.mysql.UmsAdminRoleRelationApi;
import com.itcat.mapper.UmsAdminRoleMapper;
import com.itcat.mapper.UmsAdminRoleRelationMapper;
import com.itcat.mysql.pojo.UmsResource;
import com.itcat.mysql.pojo.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/31 19:01]
 */
@DubboService
public class UmsAdminRoleRelationApiImpl implements UmsAdminRoleRelationApi {

    @Autowired
    private UmsAdminRoleMapper umsAdminRoleMapper;

    @Autowired
    private UmsAdminRoleRelationMapper mapper;

    @Autowired
    private UmsAdminRoleApi umsAdminRoleApi;

    /***
     * 获取用户所有可访问资源
     * @author Cr Mr
     * @date 2022/3/31 19:02
     */
    @Override
    public List<UmsResource> getResourceList(Long adminId) {
        List<UmsRole> umsRole = umsAdminRoleApi.getUmsRole(adminId);
        if (CollUtil.isEmpty(umsRole)) {
            return null;
        }
        List<Long> ids = CollUtil.getFieldValues(umsRole, "id", Long.class);
        return mapper.selectUmsResource(ids);
    }

    /***
     * 给角色分配资源
     * @author Cr Mr
     * @date 2022/4/3 11:19
     */
    @Override
    public void allocResource(Long roleId, List<Long> resourceIds) {
        List<Long> list = new ArrayList<>();
        list.add(roleId);
        umsAdminRoleMapper.deleteResource(list);
        mapper.allocResource(roleId, resourceIds);
    }

    /***
     * 获取角色相关资源
     * @author Cr Mr
     * @date 2022/4/3 13:09
     */
    @Override
    public List<UmsResource> listResource(Long roleId) {
        List<Long> ids = new ArrayList<>();
        ids.add(roleId);
        return mapper.selectUmsResource(ids);
    }


}
